package tms.util.logging;

import tms.util.context.ApplicationContext;

import java.io.File;

public abstract class LoggerFactoryBase {
    protected static final String LOG_RECORD_FORMAT = "{0,date,dd.MM.yyyy HH:mm:ss.SSS} {1} ThreadID={2} \"{3}\"\r\n";

    private static class Holder {
        private final static LoggerFactoryBase FACTORY;
        static {
            createLogDirectory();
            String implementation = ApplicationContext.getLoggingConfig().getImplementation();
            if ("org.apache.log4j.Logger".equals(implementation)) {
                FACTORY = new Log4jLoggerFactory();
            } else if ("java.util.logging.Logger".equals(implementation)) {
                FACTORY = new JulLoggerFactory();
            } else {
                FACTORY = new JulLoggerFactory();
            }
            FACTORY.initLoggingFramework();
        }
    }

    abstract void initLoggingFramework();

    public abstract Logger getLogger(AreaType area);

    public static LoggerFactoryBase getFactory() {
        return Holder.FACTORY;
    }


    private static void createLogDirectory() {
        String directory = ApplicationContext.getLoggingConfig().getDirectory();
        File file = new File(directory);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}
